package com.ce.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.json.JSONObject;

public class RefrectObject {
	public static void initObjForJson(Object obj, JSONObject jsonObject){
		Field[] fields = obj.getClass().getDeclaredFields();
		for(Field field : fields) {
			String fieldName = field.getName();
			if("serialVersionUID".equals(fieldName))
				continue;
			String fieldValue = "";
			String setMethodStr = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
			try{
				fieldValue = jsonObject.getString(fieldName);
			}catch(Exception e) {
				fieldValue = "";
			}
			try{
			Method method = obj.getClass().getDeclaredMethod(setMethodStr, new Class[]{field.getType()});
			method.invoke(obj, new Object[]{castFieldValue(field.getType().getSimpleName(), fieldValue)});
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static Object castFieldValue(String simpleType, String value) {
		Object obj = null;
		if("int".equals(simpleType) || "Integer".equals(simpleType)) {
			if(value == null || "".equals(value.trim()))
				value = "0";
			value = value.trim();
				try{
					obj = Integer.parseInt(value);
				}catch(NumberFormatException e) {
					throw new RuntimeException(e);
				}
		}
		
		if("float".equals(simpleType) || "Float".equals(simpleType)) {
			if(value == null || "".equals(value.trim())) 
				value = "0";
			value = value.trim();
			try{
				obj = Float.parseFloat(value);
			}catch(NumberFormatException e) {
				throw new RuntimeException(e);
			}
		}
		
		if("double".equals(simpleType) || "Double".equals(simpleType)) {
			if(value == null || "".equals(value.trim()))
				value = "0";
			value = value.trim();
			try{
				obj = Double.parseDouble(value);
			}catch(NumberFormatException e) {
				throw new RuntimeException(e);
			}
		}
		if("short".equals(simpleType) || "Short".equals(simpleType)) {
			if(value == null || "".equals(value.trim())) 
				value = "0";
			value = value.trim();
			try{
				obj = Short.parseShort(value);
			}catch(NumberFormatException e) {
				throw new RuntimeException(e);
			}
		}
		
		if("long".equals(simpleType) || "Long".equals(simpleType)) {
			if(value == null || "".equals(value.trim())) 
				value = "0";
			value = value.trim();
			try{
				obj = Long.parseLong(value);
			}catch(NumberFormatException e) {
				throw new RuntimeException(e);
			}
		} 
		
		if("String".equals(simpleType)) {
			obj = value;
		}
		
		if("byte".equals(simpleType) || "Byte".equals(simpleType)) {
			if(value == null || "".equals(value.trim())) 
				value = "0";
			value = value.trim();
			try{
				obj = Byte.parseByte(value);
			}catch(NumberFormatException e) {
				throw new RuntimeException(e);
			}
		}
		
		if("boolean".equals(simpleType) || "Boolean".equals(simpleType)) {
			if(value == null || "".equals(value.trim())) 
				value = "0";
			value = value.trim();
			if("1".equals(value) || "true".equals(value)) 
				obj = true;
			else 
				obj = false;
		}
		return obj;
	} 
}
